import 'package:flutter/cupertino.dart';
import 'package:flutter_common_fragement/common/base/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class MyListView<T extends BaseListViewModel> extends StatelessWidget {
  final T viewModel;
  final bool enablePullDown;
  final bool enablePullUp;
  final String? footerText;
  final ScrollView child;

  /// 这个controller 是保证多个tab的存在不会复用一个controller
  final RefreshController? controller;
  final Widget? header;
  final VoidCallback? onLoading;
  final VoidCallback? onRefresh;

  const MyListView({
    super.key,
    required this.viewModel,
    this.enablePullDown = true,
    this.enablePullUp = true,
    this.footerText,
    required this.child,
    this.controller,
    this.header,
    this.onRefresh,
    this.onLoading,
  });

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      controller: controller ?? viewModel.refreshController,
      enablePullDown: enablePullDown,
      enablePullUp: enablePullUp,
      onLoading: onLoading ?? viewModel.onLoading,
      onRefresh: onRefresh ?? viewModel.onRefresh,
      header: header ?? const MaterialClassicHeader(),
      footer: CustomFooter(
        builder: (BuildContext context, LoadStatus? loadStatus) {
          Widget body;
          if (loadStatus == LoadStatus.noMore) {
            body = GestureDetector(
              child: Container(
                margin: EdgeInsets.symmetric(vertical: 15.h),
                alignment: Alignment.center,
                child: Text(
                  footerText ?? '-没有更多的数据了-',
                  style: TextStyle(
                      fontSize: 16.sp, color: Theme.of(context).primaryColor),
                  textAlign: TextAlign.center,
                ),
              ),
            );
          } else {
            body = const CupertinoActivityIndicator();
          }
          return body;
        },
      ),
      child: child,
    );
  }
}
